package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectTaskStatusHistory;
import com.mxpio.erp.plm.service.impl.ProjectTaskStatusHistoryServiceImpl;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectTasksExecutionLogController", description = "任务-任务状态历史")
@RestController
@RequestMapping("/erp/plm/pro/task/status/history")
public class ProjectTaskStatusHistoryController {

    @Autowired
    private ProjectTaskStatusHistoryServiceImpl projectTaskStatusHistoryService;

    @GetMapping("list")
    @Operation(summary = "任务状态历史列表", description = "任务状态历史列表（无分页）", method = "GET")
    public Result<List<ProjectTaskStatusHistory>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectTaskStatusHistory> list = projectTaskStatusHistoryService.list(ProjectTaskStatusHistory.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{taskCode}")
    @Operation(summary = "任务状态历史列表", description = "任务状态历史列表（无分页）", method = "GET")
    public Result<List<ProjectTaskStatusHistory>> getByTaskCode(@PathVariable(name = "taskCode", required = true) String taskCode) throws UnsupportedEncodingException {
        Criteria criteria = Criteria.create().addCriterion("taskCode", Operator.EQ, taskCode);
        List<ProjectTaskStatusHistory> list = projectTaskStatusHistoryService.list(ProjectTaskStatusHistory.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "任务状态历史列表", description = "任务状态历史列表（分页）", method = "GET")
    public Result<Page<ProjectTaskStatusHistory>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectTaskStatusHistory> pageResult = projectTaskStatusHistoryService.listPage(ProjectTaskStatusHistory.class, page, criteria);
        return Result.OK(pageResult);
    }

    @RequestMapping("add")
    @Operation(summary = "保存执行任务状态历史", description = "保存执行任务状态历史")
    public Result<ProjectTaskStatusHistory> add(@RequestBody ProjectTaskStatusHistory log) {
        projectTaskStatusHistoryService.save(log);
        return Result.OK(log);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑执行任务状态历史", description = "编辑执行任务状态历史", method = "PUT")
    public Result<ProjectTaskStatusHistory> edit(@RequestBody ProjectTaskStatusHistory log) {
        projectTaskStatusHistoryService.update(log);
        return Result.OK(log);
    }

    @DeleteMapping(value = "/delete/{taskCode}")
    @Operation(summary = "删除任务状态历史",description = "删除任务状态历史", method = "DELETE")
    public Result<?> delete(@PathVariable(name = "taskCode") String taskCode) {
        projectTaskStatusHistoryService.delete(taskCode, ProjectTaskStatusHistory.class);
        return Result.OK("删除成功");
    }
}
